<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="120px"
      >
        <el-form-item label="点名次数" prop="layer" >
          <el-input v-model="formData.layer" placeholder="请输入点名次数" />
        </el-form-item>
        <el-form-item label="点名奖励类别" prop="reward_type" >
          <el-select class="w-[200px]" v-model="formData.reward_type" clearable>
              <template v-for="item in coupon_state">
                  <el-option :value="item.id" :label="item.name">{{item.name}}</el-option>
              </template>
          </el-select>
        </el-form-item>
        <el-form-item :label="'赠送'+coupon_state[formData.reward_type-1].name+'数量'" prop="layer" >
          <el-input v-model="formData.number" :placeholder="'请输入赠送'+coupon_state[formData.reward_type-1].name+'数量'" />
        </el-form-item>
<!--    赠送积分数量 需要后端接口添加     -->
		<el-form-item label="赠送优惠券" v-if="formData.reward_type==2">
		    <el-radio-group v-model="formData.reward_coupon_id">
				<template v-for="item in gradeList">
					<el-radio :label="item.id">{{item.name}}</el-radio>
				</template>
		    </el-radio-group>
		</el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { userCouponList} from "@/api/consumer";
import {addRollCallRewardRoomMz,detailRollCallRewardRoomMz,editRollCallRewardRoomMz} from "@/api/live";
import Popup from "@/components/popup/index.vue";
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑奖励" : "新增奖励";
});
const formData = reactive({
    room_id: '', // 直播间ID
    layer:1, // 点名次数
    number:'', // 赠送积分数量
    reward_type: 1, // 点名奖励类别 1 红包券 2 优惠券 3 礼品券
    reward_coupon_id: 0, // 赠送的优惠券ID
    roll_id: ''
});



const gradeList = ref([]);

userCouponList().then((res) => {
    gradeList.value = res.list.map((_res: any) => {
        return {id: _res.id, name: _res.name}
    })
});



const setID = (id:string) => {
    formData.room_id = id;
};

const coupon_state = [
	{
		id: 1,
		name: '积分'
	},
	{
		id: 2,
		name: '优惠券'
	},
	{
		id: 3,
		name: '鸡蛋'
	},
];


const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data:any[], room_id:number) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            if (key === 'room_id') {
                formData[key] = room_id
            } else {
                formData[key] = data[key]
            }

        }
    }
};

const getDetail = async (room_id:string, id:string,) => {
  const data = await detailRollCallRewardRoomMz({
    roll_id: id,
  });
    formData.roll_id = id;
  setFormData(data.info, room_id)
};




const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
    setID,
  setFormData,
  getDetail,
});
//提交
const handleSubmit = async () => {
	// formData.rank_id = formData.id
    console.log(formData)
  await formRef.value?.validate();
  mode.value == "edit"? await editRollCallRewardRoomMz(formData):await addRollCallRewardRoomMz(formData)
  popupRef.value?.close();
  emit("success");
};
</script>
